package com.lmscn.lms.api.interceptor;

import com.jfinal.aop.Invocation;
import com.jfinal.aop.PrototypeInterceptor;
import com.lmscn.lms.api.controller.CourseStudentController;

import java.util.List;

/**
 * Created by Czl on 2015/8/6.
 */
public class CollectionCondition extends PrototypeInterceptor {

	private Long studentId;
	private String type;

	private CourseStudentController csc;

	/**
	 * 对Condition的属性进行赋值初始化
	 */
	private void init(Invocation ai) {
		csc = (CourseStudentController) ai.getController();
		studentId = csc.getParaToLong("studentId");
		type = csc.getPara("type");

		if (type.equals("1")) {
			type = "3";
		}
	}

	@Override
	public void doIntercept(Invocation inv) {
		init(inv);
		csc.collectionCondition = this;
		inv.invoke();
	}

	/**
	 * 生成CourseStudentController的查询条件
	 */
	public void buildCscSql(StringBuilder sql, List<Object> paras) {
		sql.append("from t_course_student_status t where 1=1 ");
		sql.append(" and student_id = ? ");
		paras.add(studentId);
		sql.append(" and target_type = ? ");
		paras.add(type);
		
		// 添加一个降序排序 2017年3月13日16:38:05
		sql.append(" ORDER BY id DESC ");
	}
}
